function getXMLHttpRequest() {
	var XHR = null;
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") {
		XHR = new XMLHttpRequest();
	} else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
		if(browserUtente.indexOf("MSIE 5") < 0) {
    		XHR = new ActiveXObject("Msxml2.XMLHTTP");
		} else {
    		XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return XHR;
}

function fadetext(hex,tagID,color){
	if (hex<=255) {
		hex+=1;
		if (color === true) document.getElementById(tagID).style.backgroundColor="rgb("+hex+",255,"+hex+")";
		else if (color === false) document.getElementById(tagID).style.backgroundColor="rgb(255,"+hex+","+hex+")";
		if (hex == 1) setTimeout('fadetext('+hex+',"'+tagID+'",'+color+')',5000);
		else setTimeout('fadetext('+hex+',"'+tagID+'",'+color+')',20);
	} else {
		document.getElementById(tagID).style.backgroundColor="rgb(255,255,255)";
	}
}

function getParameter(queryString, parameterName) {
	//Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	if (queryString.length > 0) {
		// Find the beginning of the string
		begin = queryString.indexOf(parameterName);
		//If the parameter name is not found, skip it, otherwise return the value
		if (begin != -1) {
			//Add the length (integer) to the beginning
			begin += parameterName.length;
	        // Multiple parameters are separated by the "&" sign
	        end = queryString.indexOf("&", begin);
	        if (end == -1) {
	        	end = queryString.length
	        }
	      	//Return the string
	        return unescape(queryString.substring(begin, end));
	   }
	   // Return "null" if no parameter has been found
	   return "null";
	}
}

function connectCall() {
	document.getElementById("connectButton").innerHTML = '<img src="/spinner" alt="In progress..." />';
	document.getElementById("connectButton").disabled = true;
	 if (document.getElementById("userid").value == "") {
		 document.getElementById("resultMsg").innerHTML = "&nbsp;Insert your UserId&nbsp;";
		 fadetext(0,"resultMsg",false);
		 document.getElementById("connectButton").innerHTML = 'OK';
		 document.getElementById("connectButton").disabled = false;
		 return;
   }
	var XHR = getXMLHttpRequest();
	postStr = "hub.userid="+document.getElementById("userid").value+"&hub.mode="+document.getElementById("mode").value+"&bitly.login="+document.getElementById("bitly.login").value+"&bitly.apiKey="+document.getElementById("bitly.apiKey").value
	XHR.open('post','/pshbconnect','true');
	XHR.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	XHR.onreadystatechange = function() {
		if (XHR.readyState == 4) {
			if (XHR.status == 200) {
				if (document.getElementById("mode").value == "subscribe") window.location = "/twitterconnect/login?kname="+XHR.responseText;
				else {
					document.getElementById("resultMsg").innerHTML = "&nbsp;User Feed correctly unsubscribed&nbsp;"
					fadetext(0,"resultMsg",true);
					document.getElementById("connectButton").innerHTML = 'OK';
					document.getElementById("connectButton").disabled = false;
				}
			} else if (XHR.status == 500) {
				document.getElementById("resultMsg").innerHTML = "&nbsp;" + XHR.responseText + "&nbsp;"
				fadetext(0,"resultMsg",false)
				document.getElementById("connectButton").innerHTML = 'OK';
				document.getElementById("connectButton").disabled = false;
			} 
		}
	}
	XHR.send(postStr);
}

function setIsConnected() {
	var queryString = window.top.location.search.substring(1)
	var connected = getParameter(queryString,"connected");
	var messagge = getParameter(queryString, "messagge")
	if (connected == "true") {
		document.getElementById("resultMsg").innerHTML = "&nbsp;" + messagge + "&nbsp;"
		fadetext(0,"resultMsg",true);
		document.getElementById("connectButton").innerHTML = 'OK';
		document.getElementById("connectButton").disabled = false;
	} else if (connected == "false") {
		document.getElementById("resultMsg").innerHTML = "&nbsp;" + messagge + "&nbsp;"
		fadetext(0,"resultMsg",false);
		document.getElementById("connectButton").innerHTML = 'OK';
		document.getElementById("connectButton").disabled = false;
	}
}

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 320;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();